//작업 화면 中 정보입력 메뉴 관련 java script 정의
var now      = new Date();
var nowYear  = new Number(now.getFullYear());
var nowMonth = new Number((now.getMonth()+1)>9 ? ''+(now.getMonth()+1) : '0'+(now.getMonth()+1));            
var nowDay   = new Number(now.getDate()>9 ? ''+now.getDate():'0'+now.getDate()); 

$(document).ready(function(){
	$("#command").click(function(){
		$("#backimage").attr({src:"image.factory/back.jpg"});
		$.chartOn();
		$.chartHide();
		$.mainOff();
		$("#statsLeftMenu").css({'visibility': 'hidden'});
		$("#commandTableDiv").css({'visibility': 'visible'});
		$("#regInventoryTableDiv").css({'visibility': 'visible'});
		$("#infoInventoryTableDiv").css({'visibility': 'visible'});
		$(".commandWorkOrderDate").text(nowYear+"-"+nowMonth+"-"+nowDay);
		$.ajax({
			type : "POST",
			url : "kimchi.do",
			data : "command=getInventoryList",
			dataType:"json",
			success:function(jsonData){	
				$("#infoInventoryTableDiv").html(createInventoryInfo(jsonData.map.list));
				$("#commandWorkOrderNo").text(jsonData.map.no);
			}
		});
	});
	$("#commandWorkOrderRegBtn").click(function(){
		var quantity=$("select:[name='workorder_quantity']").val();
		$.ajax({
			type : "POST",
			url : "kimchi.do",
			data : "command=workOrder&workorder_quantity="+quantity,
			dataType:"json",
			success:function(jsonData){	
				$("#commandWorkOrderRegInfo").html("작업등록 완료 되었습니다.");
			}
		});//ajax
		$.ajax({
			type : "POST",
			url : "kimchi.do",
			data : "command=getInventoryList",
			dataType:"json",
			success:function(jsonData){	
				/*alert(jsonData.map.no);*/
				$("#commandWorkOrderNo").text(jsonData.map.no);
			}
		});//ajax
		
	});
	$("select:[name='material_name']").change(function(){
		var m=$(this).val();
		if(m==="배추"){
			$("#commandInputComanyTd").html("<select name='maker'><option value='병탁농원'>병탁농원</option><option value='장승농원'>장승농원</option></select>");
		}
		else if(m==="소금"){
			$("#commandInputComanyTd").html("<select name='maker'><option value='명석소금'>명석소금</option><option value='지훈염전'>지훈염전</option><option value='아영염전'>아영염전</option></select>");
		}
		else if(m==="양념"){
			$("#commandInputComanyTd").html("<select name='maker'><option value='주연쏘스'>주연쏘스</option><option value='희원양념'>희원양념</option></select>");
		}
		else if(m==="박스"){
			$("#commandInputComanyTd").html("<select name='maker'><option value='예슬박스'>예슬박스</option><option value='철희포장'>철희포장</option></select>");
		}
	});
	$("#regMaterialBtn").click(function(){
		var formData=$("#regInventoryForm").serialize();
		$.ajax({
			type : "POST",
			url : "kimchi.do",
			data : "command=insertInventory&"+formData,
			dataType:"json",
			success:function(jsonData){
				$("#infoInventoryTableDiv").html(createInventoryInfo(jsonData.list));
			}
		});
	});
});

function createInventoryInfo(data){
	var table="<form><table><caption>등록된 재고정보</caption><thead><tr><th>품목</th>"
		+"<th>입고날짜</th><th>거래처</th><th>수량</th></tr></thead>";
		for(var i=0;i<data.length;i++){
		table+="<tbody><tr>"
		+"<td>"+data[i].material_name+"</td>"
		+"	<td>"+data[i].inventory_date+"</td>"
		+"	<td>"+data[i].maker+"</td>"
		+"	<td>"+data[i].input_quantity+"</td>"
		+"</tr></tbody>"; 
		}
		table+="</table></form>";
	return table;
}
